//service统一出口
import PXRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'
import localCache from '@/utils/cache'

const pxRequest = new PXRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config) => {
      //携带token
      const token = localCache.getCache('token')
      if (token) {
        config.headers.Authorization = `Bearer ${token}`
      }
      console.log('pxRequest对象请求成功的拦截')
      return config
    },

    requestnterceptorCatch: (error) => {
      console.log('pxRequest对象请求失败的拦截')
      return error
    },

    responseInterceptor: (res) => {
      console.log('pxRequest对象响应成功的拦截')
      return res
    },

    responseInterceptorCatch: (err) => {
      console.log('pxRequest对象响应失败的拦截')
      console.log(err)
      return err
    }
  }
})

export default pxRequest
